package com.csun.dsms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFactoryPredictionDataMapper;
import com.csun.dsms.model.huang.HgFactoryPredictionData;
import com.csun.dsms.service.HgFactoryPredictionDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class HgFactoryPredictionDataServiceImpl implements HgFactoryPredictionDataService {

    @Resource
    private HgFactoryPredictionDataMapper hgFactoryPredictionDataMapper;

    @Override
    public Wrapper importFactoryPredictionData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFactoryPredictionData hgFactoryPredictionData = new HgFactoryPredictionData();
                hgFactoryPredictionData.setStatisticalYear(Convert.toInt(excelMap.get("statistical_year")));
                hgFactoryPredictionData.setStatisticalMonth(Convert.toInt(excelMap.get("statistical_month")));
                hgFactoryPredictionData.setStatisticalQuarter(Convert.toInt(excelMap.get("statistical_quarter")));
                hgFactoryPredictionData.setArea(Convert.toStr(excelMap.get("area")));
                hgFactoryPredictionData.setAreaLevel(Convert.toInt(excelMap.get("area_level")));
                hgFactoryPredictionData.setType(Convert.toInt(excelMap.get("type")));
                hgFactoryPredictionData.setTotal(Convert.toDouble(excelMap.get("total")));
                hgFactoryPredictionData.setGrowthRate(Convert.toDouble(excelMap.get("growth_rate")));
                hgFactoryPredictionData.setRanking(Convert.toInt(excelMap.get("ranking")));
                hgFactoryPredictionData.setEntryDepartment(Convert.toStr(excelMap.get("entry_department")));
                hgFactoryPredictionData.setCreator(Convert.toStr(excelMap.get("creator")));
                hgFactoryPredictionData.setApproved(Convert.toInt(excelMap.get("approved")));
                hgFactoryPredictionData.setApproval(Convert.toStr(excelMap.get("approval")));
                hgFactoryPredictionData.setCreatedTime(DateUtil.parse(Convert.toStr(excelMap.get("created_time")), DatePattern.NORM_DATETIME_PATTERN));
                hgFactoryPredictionDataMapper.insert(hgFactoryPredictionData);

            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入数据成功");
        } catch (Exception e) {
            log.error("导入数据报错", e);
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
